#include "game.h"
#include <QPainter>
#include <QPushButton>
#include <QTimer>
#include <grid.h>
#include <vector>
#include <QDebug>
#include <QLabel>
#include <QtGlobal>
#include <QTime>
#include <qfont.h>
#include <QDialog>
#include <QMenuBar>
#include <dialog.h>
game::game(QWidget *parent,int length,int width,int num) : QMainWindow(parent)
{ 
    QMenuBar * bar = menuBar();
    setMenuBar(bar);
    QMenu * startMenu = bar->addMenu("开始");

    QAction * quit = startMenu->addAction("退出");
    connect(quit,&QAction::triggered,[=](){
        this->close();
    });
    QAction * back = startMenu->addAction("返回");
    connect(back,&QAction::triggered,[=](){
        emit this->Backtomenu();
    });
    this->setFixedSize(length*40+80,width*40+80);
    bool map[50][50];
    for(int i = 0;i < 50;i ++){
        for(int j = 0;j < 50;j ++){
            map[i][j] = false;
        }
    }
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    for(int i = 0;i < num;i ++){
        int x = qrand() % length;
        int y = qrand() % width;
        if(!map[x][y]){
            map[x][y] = true;
        }
        else{
            i --;
        }
    }//生成num个雷
    QFont font;
    font.setFamily("Gungsuh");
    font.setPointSize(20);
    for(int i = 0;i < length;i ++){
        for(int j = 0;j < width;j ++){
            if(map[i][j]){
                grid * btn = new grid(i,j,0);
                btn -> setParent(this);
                btn -> move(i*40+40,j*40+40);
                minemap[i][j] = btn;
            }
            else{
                grid * btn = new grid(i,j,1);
                btn -> setParent(this);
                btn -> move(i*40+40,j*40+40);
                minemap[i][j] = btn;
            }
            connect(minemap[i][j],&QPushButton::clicked,[=](){
                if(minemap[i][j]->finished) return;
                if(!minemap[i][j]->type){//游戏失败界面
                    for(int i = 0;i < length;i ++){
                        for(int j = 0;j < width;j ++){
                            minemap[i][j]->finished = true;
                            if(minemap[i][j]->type == 0){
                                minemap[i][j] -> hide();
                                QLabel * label = new QLabel;
                                label->setParent(this);
                                label->setFont(font);
                                label->setText("*");
                                label->setFixedSize(40,40);
                                label->move(i*40+40,j*40+40);
                                label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                                label->show();
                            }
                        }
                    }
                    QTimer::singleShot(200,this,[=](){//延时//失败对话框
                        dialog * failed = new dialog("游戏失败！！");
                        failed->show();
                        connect(failed,&dialog::Backtomenu,this,[=](){
                            emit this -> Backtomenu();
                            failed->hide();
                            this->hide();
                        });
                    });
                }
                else{
                    dfs(minemap[i][j]->posx,minemap[i][j]->posy,length,width);//搜索，翻开其他相邻的
                }
                //判断游戏是否 胜利
                if(isWin(length,width)){
                    qDebug() <<"WIN!!!!!!!!";//处理游戏胜利
                };
            });

        }
    }
}
bool game::isWin(int length,int width){
    int ans = 0;
    for(int i = 0;i < length;i ++){
        for(int j = 0;j < width;j ++){
            ans += minemap[i][j]->nowtype;
        }
    }
    if(ans == 0) return true;
    else return false;
}
bool game::infield(int x,int y,int length,int width){
    if(x < length && x >= 0 && y < width && y >= 0) return true;
    else return false;
}
void game::dfs(int x,int y,int length,int width){
    int num = 0;
    minemap[x][y] -> nowtype = 0;
    for(int k = 0;k < 8;k ++){
        if(infield(x + dx[k],y + dy[k],length,width)&&
        minemap[x + dx[k]][y + dy[k]] -> type == 0){
           num++;
        }
    }
    minemap[x][y] -> hide();
    if(num == 0){//白板，搜索周围的方块
        for(int k = 0;k < 8;k ++){
            if(infield(x + dx[k],y + dy[k],length,width) &&
            minemap[x + dx[k]][y + dy[k]] -> nowtype != 0){
                dfs(x + dx[k],y + dy[k],length,width);
            }
        }
        return;
    }
    else{
        QLabel * label = new QLabel;
        QFont font1;
        font1.setFamily("Gungsuh");
        font1.setPointSize(20);
        label->setFont(font1);
        label->setParent(this);
        label->setText(QString::number(num));
        label->setFixedSize(40,40);
        label->move(x*40+40,y*40+40);
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        label->show();
        return;
    }
}
